<template>
  <view class="comment-container">
    <u-tabs :list="tabList" :current="current" @change="handleTabChange"></u-tabs>
    
    <view class="comment-list" v-if="commentList.length > 0">
      <view class="comment-item" v-for="(item, index) in commentList" :key="index">
        <view class="item-header">
          <view class="article-title" @click="goArticle(item)">{{ item.archives_title }}</view>
          <view class="time">{{ item.createtime }}</view>
        </view>
        <view class="item-content">
          <view class="comment-text">{{ item.content }}</view>
        </view>
        <view class="item-footer">
          <view class="stats">
            <view class="stat-item">
              <u-icon name="thumb-up" size="28" color="#999"></u-icon>
              <text>{{ item.likes || 0 }}</text>
            </view>
          </view>
          <view class="actions">
            <u-button size="mini" type="primary" @click="replyComment(item)">回复</u-button>
            <u-button size="mini" type="error" @click="deleteComment(item)">删除</u-button>
          </view>
        </view>
        
        <!-- 回复列表 -->
        <view class="reply-list" v-if="item.replyList && item.replyList.length > 0">
          <view class="reply-item" v-for="(reply, replyIndex) in item.replyList" :key="replyIndex">
            <view class="reply-user">{{ reply.username }}：</view>
            <view class="reply-content">{{ reply.content }}</view>
          </view>
        </view>
      </view>
    </view>
    
    <u-empty v-else text="暂无评论" mode="comment"></u-empty>
    
    <u-loadmore :status="loadStatus" />
    
    <!-- 删除确认弹窗 -->
    <u-modal
      v-model="showDeleteModal"
      title="删除确认"
      content="确定要删除这条评论吗？删除后无法恢复。"
      :show-cancel-button="true"
      @confirm="confirmDelete"
    ></u-modal>
    
    <!-- 回复弹窗 -->
    <u-popup v-model="showReplyPopup" mode="bottom">
      <view class="reply-popup">
        <view class="reply-header">
          <view class="title">回复评论</view>
          <view class="close" @click="showReplyPopup = false">
            <u-icon name="close" size="30"></u-icon>
          </view>
        </view>
        <view class="reply-body">
          <u-textarea v-model="replyContent" placeholder="请输入回复内容" :maxlength="200"></u-textarea>
        </view>
        <view class="reply-footer">
          <u-button type="primary" @click="submitReply">提交回复</u-button>
        </view>
      </view>
    </u-popup>
  </view>
</template>

<script>
import { comment } from '@/api/cms'

export default {
  data() {
    return {
      tabList: [
        { name: '我的评论' },
        { name: '收到的评论' }
      ],
      current: 0,
      commentList: [],
      page: 1,
      limit: 10,
      loadStatus: 'loadmore',
      showDeleteModal: false,
      currentDeleteItem: null,
      showReplyPopup: false,
      replyContent: '',
      currentReplyItem: null
    }
  },
  onLoad() {
    this.getCommentList()
  },
  onPullDownRefresh() {
    this.page = 1
    this.commentList = []
    this.getCommentList().then(() => {
      uni.stopPullDownRefresh()
    })
  },
  onReachBottom() {
    if (this.loadStatus === 'loadmore') {
      this.page++
      this.getCommentList()
    }
  },
  methods: {
    // 获取评论列表
    async getCommentList() {
      try {
        this.loadStatus = 'loading'
        
        const params = {
          page: this.page,
          limit: this.limit,
          type: this.current === 0 ? 'my' : 'received'
        }
        
        const { data } = await comment.getCommentList(params)
        
        if (data.code === 1) {
          if (this.page === 1) {
            this.commentList = data.data.rows
          } else {
            this.commentList = [...this.commentList, ...data.data.rows]
          }
          
          this.loadStatus = data.data.rows.length < this.limit ? 'nomore' : 'loadmore'
        } else {
          this.loadStatus = 'loadmore'
          uni.showToast({
            title: data.msg || '获取评论列表失败',
            icon: 'none'
          })
        }
      } catch (error) {
        console.error('获取评论列表失败', error)
        this.loadStatus = 'loadmore'
        uni.showToast({
          title: '获取评论列表失败，请稍后再试',
          icon: 'none'
        })
      }
    },
    
    // 处理标签页切换
    handleTabChange(index) {
      this.current = index
      this.page = 1
      this.commentList = []
      this.getCommentList()
    },
    
    // 跳转到文章详情
    goArticle(item) {
      uni.navigateTo({
        url: `/pages_article/detail/index?id=${item.archives_id}`
      })
    },
    
    // 回复评论
    replyComment(item) {
      this.currentReplyItem = item
      this.replyContent = ''
      this.showReplyPopup = true
    },
    
    // 提交回复
    async submitReply() {
      if (!this.replyContent.trim()) {
        uni.showToast({
          title: '请输入回复内容',
          icon: 'none'
        })
        return
      }
      
      if (!this.currentReplyItem) return
      
      try {
        const params = {
          pid: this.currentReplyItem.id,
          archives_id: this.currentReplyItem.archives_id,
          content: this.replyContent
        }
        
        const { data } = await comment.addComment(params)
        
        if (data.code === 1) {
          uni.showToast({
            title: '回复成功',
            icon: 'success'
          })
          
          // 关闭弹窗
          this.showReplyPopup = false
          
          // 刷新评论列表
          this.page = 1
          this.commentList = []
          this.getCommentList()
        } else {
          uni.showToast({
            title: data.msg || '回复失败',
            icon: 'none'
          })
        }
      } catch (error) {
        console.error('回复评论失败', error)
        uni.showToast({
          title: '回复失败，请稍后再试',
          icon: 'none'
        })
      } finally {
        this.currentReplyItem = null
      }
    },
    
    // 删除评论
    deleteComment(item) {
      this.currentDeleteItem = item
      this.showDeleteModal = true
    },
    
    // 确认删除
    async confirmDelete() {
      if (!this.currentDeleteItem) return
      
      try {
        const { data } = await comment.deleteComment(this.currentDeleteItem.id)
        
        if (data.code === 1) {
          uni.showToast({
            title: '删除成功',
            icon: 'success'
          })
          
          // 从列表中移除已删除的评论
          const index = this.commentList.findIndex(item => item.id === this.currentDeleteItem.id)
          if (index !== -1) {
            this.commentList.splice(index, 1)
          }
        } else {
          uni.showToast({
            title: data.msg || '删除失败',
            icon: 'none'
          })
        }
      } catch (error) {
        console.error('删除评论失败', error)
        uni.showToast({
          title: '删除失败，请稍后再试',
          icon: 'none'
        })
      } finally {
        this.currentDeleteItem = null
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.comment-container {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding-bottom: 30rpx;
}

.comment-list {
  padding: 20rpx;
}

.comment-item {
  background-color: #ffffff;
  border-radius: 12rpx;
  padding: 20rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
}

.item-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16rpx;
  
  .article-title {
    font-size: 28rpx;
    color: #3c9cff;
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  
  .time {
    font-size: 24rpx;
    color: #999999;
    margin-left: 20rpx;
  }
}

.item-content {
  margin-bottom: 16rpx;
  
  .comment-text {
    font-size: 30rpx;
    color: #333333;
    line-height: 1.5;
  }
}

.item-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  
  .stats {
    display: flex;
    
    .stat-item {
      display: flex;
      align-items: center;
      margin-right: 20rpx;
      
      text {
        font-size: 24rpx;
        color: #999999;
        margin-left: 6rpx;
      }
    }
  }
  
  .actions {
    display: flex;
    
    .u-button {
      margin-left: 20rpx;
    }
  }
}

.reply-list {
  margin-top: 20rpx;
  background-color: #f9f9f9;
  border-radius: 8rpx;
  padding: 16rpx;
}

.reply-item {
  display: flex;
  margin-bottom: 10rpx;
  
  &:last-child {
    margin-bottom: 0;
  }
  
  .reply-user {
    font-size: 26rpx;
    color: #3c9cff;
    margin-right: 10rpx;
  }
  
  .reply-content {
    font-size: 26rpx;
    color: #333333;
    flex: 1;
  }
}

.reply-popup {
  background-color: #ffffff;
  padding: 30rpx;
  
  .reply-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30rpx;
    
    .title {
      font-size: 32rpx;
      font-weight: bold;
      color: #333333;
    }
  }
  
  .reply-body {
    margin-bottom: 30rpx;
  }
}
</style>